package com.dxk.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class helloShrio {

    @RequestMapping({"/","/index"})
    public String toIndex(Model model){
        model.addAttribute("msg","hello");

        return "index";
    }


    @RequestMapping("/add")
    public  String add(){

        return "view/add";
    }
    @RequestMapping("/update")
    public  String update(){

        return "view/update";
    }

    public String login(String username,String password,Model model) {

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);

        try {
            subject.login(token);
            return "index";
        } catch (UnknownAccountException e){
            model.addAttribute("user","账号错误");
            return "login";
        }catch (IncorrectCredentialsException e){
            model.addAttribute("user","密码错误");
            return "login";
        }


    }

}
